﻿using System;
using MassPokerTool.Infrastructure.Extensions;
using MassPokerTool.Infrastructure.Hooking.Data;
using MassPokerTool.Infrastructure.Logging;
using MassPokerTool.Infrastructure.Ocr;

namespace MassPokerTool.Infrastructure.Hooking.Handlers
{
    public class ScreenCaptureBitBltHookHandler : IHookHandler
    {
        private readonly ILogger _log;
        private readonly ITextRecognizer<ScreenCaptureBitBltHookData> _textRecognizer;

        public ScreenCaptureBitBltHookHandler(ILogger log, ITextRecognizer<ScreenCaptureBitBltHookData> textRecognizer)
        {
            _log = log;
            _textRecognizer = textRecognizer;
        }

        public void Handle(IHookData parameter)
        {
            var hookData = parameter as ScreenCaptureBitBltHookData;
            if (hookData != null)
            {
                try
                {
                    var recognizedText = _textRecognizer.RecognizeText(hookData);
                    var newTexts = PokerstarsChatTextEvaluator.Evaluate(recognizedText);
                    if (!newTexts.IsNullOrEmpty())
                        MessageBus.AddMessages("Chat", newTexts);
                    _log.Debug(string.Format("ScreenCapture -> WindowName: {0}", hookData.WindowName));
                }
                catch (Exception e)
                {
                    _log.Error(e.ToString());
                }          
            }
        }
    }
}
